﻿using System;
using System.Linq;
using System.Linq.Expressions;
using SBACN.WebSite.Code;
using SBACN.WebSite.Models;
using System.Collections.Generic;

namespace SBACN.WebSite.ContentDL
{
	public class SelYearItem
	{
		public string name { get; set; }
	}
	public partial class ComList : MemberBasePage
	{
		protected string NoContent
		{
			get
			{
				if (ViewState["NoContent"] == null)
				{
					return string.Empty;
				}

				return ViewState["NoContent"].ToString();
			}
			set
			{
				ViewState["NoContent"] = value;
			}
		}
		//按年份进行过滤
		protected string Filter { get; set; }
		protected string Img { get; set; }
		protected void Page_Load(object sender, EventArgs e)
		{
			ImgData();
			this.NoContent = Request.QueryString["nc"];
			this.Filter = Request.QueryString["ft"];
			if (!Page.IsPostBack)
			{
				if (!string.IsNullOrWhiteSpace(this.Filter))
				{
					SelItemYearData();
				}
				BindData(1, "");
			}
			else
			{
				BindDropDownList();
			}
		}

		protected void BindData(int pageIndex, string selItem)
		{
			int records = 0;
			Expression<Func<TB_News, bool>> f;
			if (string.IsNullOrWhiteSpace(selItem))
			{
				f = (p => p.Type == this.SystemCode && !p.IsEn);

			}
			else
			{
				DateTime StartDate = Convert.ToDateTime(selItem + "-01-01 00:00:01");
				DateTime EndDate = Convert.ToDateTime(selItem + "-12-31 23:59:59");
				f = f = (p => p.Type == this.SystemCode && !p.IsEn && p.CreateDate > StartDate && p.CreateDate < EndDate);
			}
			Expression<Func<TB_News, DateTime>> order = (c => c.CreateDate);
			Expression<Func<TB_News, int>> then = (p => p.ID);
			var resultC = DAL.DALHelper.Instance.GetList<TB_News, DateTime, int>(f, order, false, then, false, this.Pager1.PageSize, pageIndex, out records);
			//if (resultC.Count > 0)
			{
				this.CouncilList.DataSource = resultC;
				this.CouncilList.DataBind();
				this.Pager1.RecordCount = records;
				this.Pager1.InitControl();
			}
		}

		protected void BindSelectedPageIndexChanged(int pageIndex, int nextPage)
		{
			string Sel = this.WeekReport.SelectedValue;
			BindData(nextPage, Sel);
		}

		protected void ImgData()
		{
			string type;
			if (string.IsNullOrWhiteSpace(this.SystemCode))
			{
				//TODO 默认图片
				type = "";
			}
			else
			{
				type = this.SystemCode;
			}
			Expression<Func<TB_SystemConfig, bool>> f = (p => p.Code == "Img" && p.Type == type);
			var resultQ = DAL.DALHelper.Instance.GetFirstOrDefaultObject<TB_SystemConfig>(f);
			if (resultQ != null)
			{
				this.Img = resultQ.Value.ToString();
			}
		}

		protected void SelItemYearData()
		{
			this.SelItem.Visible = true;
			int StartYearData = Convert.ToInt32(DateTime.Now.ToString("yyyy"));
			int EndYearData = Convert.ToInt32(DateTime.Now.ToString("yyyy"));
			Expression<Func<TB_News, bool>> f = (p => p.Type == this.SystemCode && !p.IsEn);
			Expression<Func<TB_News, DateTime>> order = (c => c.CreateDate);
			var resultC = DAL.DALHelper.Instance.GetList<TB_News, DateTime>(f, order, true);

			if (resultC.Count > 0)
			{
				var resultYear = resultC.Select(c => new { c.CreateDate.Year }).Distinct().ToList();

				this.WeekReport.DataSource = resultYear;
				this.WeekReport.DataTextField = "Year";
				this.WeekReport.DataValueField = "Year";
				this.WeekReport.DataBind();

			}

		}

		protected void BindDropDownList()
		{
			string Sel = this.WeekReport.SelectedValue;
			BindData(1, Sel);
		}

	}
}